struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};

#include <queue>
using namespace std;
class Solution
{
public:
    bool isEvenOddTree(TreeNode *root)
    {
        // 利用bfs的性质
        queue<TreeNode *> q;
        q.push(root);
        int level = 0, level_size = 1;
        int pre = 0;
        while (q.size())
        {
            // cout << pre << ' '  << level << endl;
            auto e = q.front();
            q.pop();
            level_size--;
            // 如果是奇数层，就要看是否是偶整数，并且还需要严格递减
            if (level % 2)
            {
                if (e->val % 2)
                    return false;
                if (pre && pre <= e->val)
                    return false;
                pre = e->val;
            }
            else
            {
                // 在偶数层，判断是不是奇数，并且严格递增
                if (e->val % 2 == 0)
                    return false;
                if (pre && pre >= e->val)
                    return false;
                pre = e->val;
            }
            if (e->left)
                q.push(e->left);
            if (e->right)
                q.push(e->right);
            if (!level_size)
            {
                pre = 0;
                ++level;
                level_size = q.size();
            }
        }
        return true;
    }
};